package com.yanqu.road.dao.impl.tradewar;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.dao.impl.logic.ResultSetParseEntityManger;
import com.yanqu.road.entity.tradewar.TradeWarWanted;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossTradeWarWantedDaoImpl extends TempDataObjectDao<TradeWarWanted> {

	public Map<String, TradeWarWanted> getTradeWarWantedMapByActivityId(int activityId) {
		ResultSet rs = null;
		CallableStatement call = null;
		Map<String, TradeWarWanted> dataMap = new ConcurrentHashMap<>();

		String sql = "select * from t_c_trade_war_wanted where activityId = ? ";
		try {
			getDb().setConnName(DbNameStrategy.getCross2Db());
			call = getDb().getConnection().prepareCall(sql);
			call.setInt(1,activityId);
			rs = call.executeQuery();
			while (rs.next()) {
				TradeWarWanted data = parse(rs);
				dataMap.put(data.getWantedUid(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public Map<Integer, Map<String, TradeWarWanted>> getTradeWarWantedMap(List<Integer> activityIdList) {
		ResultSet rs = null;
		CallableStatement call = null;
		Map<Integer, Map<String, TradeWarWanted>> dataMap = new ConcurrentHashMap<>();
		if(null == activityIdList || activityIdList.size() <= 0){
			return dataMap;
		}
		String sql = "select * from t_c_trade_war_wanted where activityId in("+ StringUtils.listToString(activityIdList, ",") + ")";
		try {
			getDb().setConnName(DbNameStrategy.getCross2Db());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				TradeWarWanted data = parse(rs);
				if(!dataMap.containsKey(data.getActivityId())){
					dataMap.put(data.getActivityId(), new ConcurrentHashMap<>());
				}
				dataMap.get(data.getActivityId()).put(data.getWantedUid(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	@Override
	protected boolean addData(TradeWarWanted data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_c_trade_war_wanted(activityId,wantedUid,userId,userBaseInfo,wantedUserId,wantedBaseInfo,wantedScore,wantedTime,battleTimes,"
				+ "reward,finishTime,scoreReward,useItemId) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getCross2Db());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setInt(i++, data.getActivityId());
			call.setString(i++, data.getWantedUid());
			call.setLong(i++, data.getUserId());
			call.setString(i++, data.getUserBaseInfoJsonStr());
			call.setLong(i++, data.getWantedUserId());
			call.setString(i++, data.getWantedBaseInfoJsonStr());
			call.setLong(i++, data.getWantedScore());
			call.setLong(i++, data.getWantedTime());
			call.setInt(i++, data.getBattleTimes());
			call.setString(i++, data.getReward());
			call.setLong(i++, data.getFinishTime());
			call.setBoolean(i++, data.isScoreReward());
			call.setInt(i++, data.getUseItemId());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(TradeWarWanted data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_c_trade_war_wanted SET activityId=?,userId=?,userBaseInfo=?,wantedUserId=?,wantedBaseInfo=?,wantedScore=?,wantedTime=?,"
				+ "battleTimes=?,reward=?,finishTime=?,scoreReward=?,useItemId=? WHERE wantedUid=?";
		try {
			getDb().setConnName(DbNameStrategy.getCross2Db());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setInt(i++, data.getActivityId());
			call.setLong(i++, data.getUserId());
			call.setString(i++, data.getUserBaseInfoJsonStr());
			call.setLong(i++, data.getWantedUserId());
			call.setString(i++, data.getWantedBaseInfoJsonStr());
			call.setLong(i++, data.getWantedScore());
			call.setLong(i++, data.getWantedTime());
			call.setInt(i++, data.getBattleTimes());
			call.setString(i++, data.getReward());
			call.setLong(i++, data.getFinishTime());
			call.setBoolean(i++, data.isScoreReward());
			call.setInt(i++, data.getUseItemId());
			call.setString(i++, data.getWantedUid());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected TradeWarWanted parseData(ResultSet rs) throws SQLException {
		TradeWarWanted data = ResultSetParseEntityManger.initTradeWarWanted(rs);
		data.setActivityId(rs.getInt("activityId"));
		return data;
	}
}
